package com.hk.servlet.company;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.CompanyMessage;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.ICompanyMessageService;
import com.hk.service.impl.CompanyMessageServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/11 15:02
 * @Statement: 查询公司信息和添加信息
 */
@WebServlet("/company/CompanyMessageQueryOrSave")
public class CompanyMessageQueryOrSaveServlet extends HttpServlet {

    ICompanyMessageService iCompanyMessageService = new CompanyMessageServiceImpl();

    /**
     * 查询公司信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String startTime = Tool.nullToString(req.getParameter("startTime"));
        String endTime = Tool.nullToString(req.getParameter("endTime"));
        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
        //        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
//        封装对象
        CompanyMessage companyMessage = new CompanyMessage();
        companyMessage.setStartTime(startTime);
        companyMessage.setEndTime(endTime);
//        查询数据
        List<CompanyMessage> companyMessageList = iCompanyMessageService.findAll(pageNum, size, companyMessage);
//        查询总数
        int count = iCompanyMessageService.count(-1,-1, companyMessage);
        PrintWriter out = resp.getWriter();
        if (companyMessageList.isEmpty()) {
            ResultVO<CompanyMessage> resultVO = new ResultVO<>(0,"error",0,companyMessageList);
            String companyMessageListJSON = JSONObject.toJSONString(resultVO);
            out.println(companyMessageListJSON);
        } else {
            ResultVO<CompanyMessage> resultVO = new ResultVO<>(0,"success",count,companyMessageList);
            String companyMessageListJSON = JSONObject.toJSONString(resultVO);
            out.println(companyMessageListJSON);
        }
        out.flush();
        out.close();


    }

    /**
     * 添加公司信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson= new Gson();
        CompanyMessage companyMessage = gson.fromJson(reader, CompanyMessage.class);
        int save = iCompanyMessageService.save(companyMessage);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (save != 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();
    }
}
